package com.jctrip.hotel.source.xxl.dossen;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jctrip.hotel.common.constant.XxlJobNameConstant;
import com.jctrip.hotel.common.util.JsonUtil;
import com.jctrip.hotel.domain.dao.HtSourceInfoDao;
import com.jctrip.hotel.domain.entity.HtSourceInfo;
import com.jctrip.hotel.source.model.CommonJobParam;
import com.jctrip.hotel.source.service.dossen.DossenTaskService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author: zxy
 * @CreateTime: 2023-09-04 20:33
 * @Description:
 */
@Component
@Slf4j
public class SyncDossenLongerRatesPopXxlJob extends IJobHandler {
    @Resource
    private DossenTaskService dossenTaskService;
    @Resource
    private HtSourceInfoDao htSourceInfoDao;

    @Override
    @XxlJob(XxlJobNameConstant.SYNC_DOSSEN_LONGER_RATES_TASK_POP)
    public void execute() throws Exception {
        XxlJobHelper.log(XxlJobNameConstant.SYNC_DOSSEN_LONGER_RATES_TASK_POP + " xxl job start");
        try {
            CommonJobParam commonJobParam = JsonUtil.toObject(CommonJobParam.class, XxlJobHelper.getJobParam());
            HtSourceInfo htSourceInfo =
                    htSourceInfoDao.selectOne(new LambdaQueryWrapper<HtSourceInfo>().eq(HtSourceInfo::getSourceId,
                            commonJobParam.getSourceId()));
            if (htSourceInfo == null) {
                return;
            }
            dossenTaskService.syncDossenLongerRatesTaskFastPop(htSourceInfo.getSourceAppId(), htSourceInfo.getSourceSecret(),
                    commonJobParam.getAddDays(), commonJobParam.getDays(),
                    (Integer) JsonUtil.toMap(htSourceInfo.getExtra()).get("cardLevel"),
                    (String) JsonUtil.toMap(htSourceInfo.getExtra()).get("activityCode"));
        } catch (Exception e) {
            log.error(XxlJobNameConstant.SYNC_DOSSEN_LONGER_RATES_TASK_POP + "error ", e);
        }
        XxlJobHelper.log(XxlJobNameConstant.SYNC_DOSSEN_LONGER_RATES_TASK_POP + " xxl job end");

    }
}